3. Sqlcmd
If you've worked with SQL
Server for any length of time, chances are you've worked with the sqlcmd
utility. This utility lets you enter and execute T-SQL statements and
other objects via a command prompt. You can also use the sqlcmd utility
via the Query Editor in sqlcmd mode, in a Windows script file, or via a
SQL Server Agent job.
This section discusses how to
use the sqlcmd utility to connect to a SQL Azure database and execute
queries against that database. This section assumes that you have some
familiarity with sqlcmd. This utility has many options, or parameters,
but this section only discusses those necessary to connect to SQL Azure.
NOTE
SQL Azure doesn't support the -z or -Z option for changing user passwords. You need to use ALTER LOGIN after connecting to the master database in order to change a password.
To use the sqlcmd utility, you
first open a command prompt. At the command prompt, you need to provide
the options and values necessary to connect to the SQL Azure database.
As a minimum, the command syntax is the following:
sqlcmd -U login -P password -S server -d database
The parameters are nearly self-explanatory, but here they are, just in case:
Optionally, you can provide a database name via the -d parameter. Thus, the sqlcmd syntax looks something like the following:
Sqlcmd -U providerlogin@Server -P ProviderPassword -S ProviderServer -d database
Let's put this syntax to use. Follow these steps:
At the command prompt, use the sqlcmd syntax and type in your connection information, as shown in Figure 3. (In the figure, the server name and password are hidden.) Press Enter.
When the sqlcmd utility connects, you're presented with the sqlcmd prompt 1>, at which point you can begin typing in and executing T-SQL commands. The command to execute any T-SQL statement is GO. For example, in Figure 4, the following SELECT statement is entered and executed:
SELECT Name FROM Users
Press the Enter key on line 1> to create a new line. Pressing Enter executes the SELECT query. Type GO on line 2> and press Enter, to execute all statements since the last GO statement (see Figure 4). Figure 5 shows the results of the sqlcmd query entered. As you can see, executing a query isn't difficult.
Let's work through another example in which you create a table and add data. Here are the steps:
After the previous query is finished, you're back at the 1> prompt. Type in the statement shown in Figure 6.
Press Enter, type GO on line 2>, and press Enter again, to execute the CREATE statement.
When
the T-SQL command that you execute is the type that doesn't return
data, the sqlcmd utility doesn't give you back a message but takes you
to the 1>Figure 7 shows the results from doing that—you can see that the table was indeed created. prompt.
However, you can verify that a statement executed successfully by going
into SQL Server Management Studio (SSMS), connecting to your SQL Azure
instance, and expanding the Tables node of your chosen database.
The table you created is
called TechGeoInfo, and it has three columns: an ID column that is the
primary key (clustered index), a TechID column, and an address column.
The table is simple, but it's good enough to demonstrate functionality.
Add some data to the table by going back to the command window and typing in the INSERT statements shown in Figure 8.
The great thing about the sqlcmd utility is that you can enter in as
many commands as you want and not execute them until you type GO. Here you use two INSERT statements that add two records the table you created in the previous step.
Type GO on line 3>, and press Enter. Although the sqlcmd utility tells you 1 rows affected, you can query this new table in SSMS and see the two new rows that were added, as shown in Figure 9.
As you can see, using the
sqlcmd utility is straightforward. Just remember that it doesn't work
with SQL Azure if you're trying to use heap tables. All tables must have
a primary key. Also, as mentioned earlier, the -z and -Z parameters don't work.
This section has
discussed the different mechanisms for connecting and querying SQL
Azure, including examples for ADO.NET, ODBC, and SqlCmd. You can see
that it quite similar to the way you currently connect to and query an
on-premise database. However, with an overall industry push to an SOA
architecture, let's take the discussion to the next level and discuss
using services, specifically WCF Data Services, to connect to our Azure
database.